from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse, redirect

import medical.middleware.auth

# 中间件实现登录检验
# 使用中间件去setting middelware
# 'app03.middelware.auth.M1'

# class M1(MiddlewareMixin):
#     # 中间件
#     def process_request(self,request):
#         print("M1.进来了")

class AuthMiddleware(MiddlewareMixin):

    def process_request(self, request):
        # 0.排除那些不需要登录就能访问的页面
        #   request.path_info 获取当前用户请求的URL /login/
        if request.path_info in ["/login/", "/image/code/","/cases/list/"]:
        # if request.path_info == "/login/":
            return
            # return 相当于return None

        # 1.读取当前访问的用户的session信息，如果能读到，说明已登陆过，就可以继续向后走。
        info_dict = request.session.get("info")
        # print(info_dict)
        if info_dict:
            return

        # 2.没有登录过，重新回到登录页面
        return redirect('/login/')
